{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Calculate the grade average of the class\n", "\n", "We have a text file with the marks of the class students. Each row contains information for a different student. Each row has the following information:\n", "`identifier,username,note`\n", "each field separated by a comma (CSV). Fields cannot have commas internally, because it would be considered a field separation.\n", "\n", "For example,\n", "```csv\n", "1,frasalca,6.5\n", "2,romelco,9.1\n", "3,beaguma,7.4\n", "4,pacosa,8.6\n", "```\n", "\n", "To create a text file, press the **+** button and select *Text file*. Write the content and save it with a name, for example, *marks.csv*.\n", "\n", "In Python, in order to read a file it is necessary to open it first (and when we finish, it ha to be closed). You can use:\n", "\n", "```python\n", "f = open(\"marks.csv\")\n", "```\n", "\n", "To open the file, and:\n", "\n", "```python\n", "f.close()\n", "```\n", "\n", "To close it.\n", "\n", "\n", "Once the file is opened with the `open()` function and before closing it, it can be read line by line with the `f.readline()` function. For example:\n", "\n", "```line = f.readline()``\n", "\n", "will assign the variable `line` with the first line of the file.\n", "\n", "If the `line` variable is not empty, it means that we have not reached the end of the file yet. Therefore, to iterate through the lines of the file we can use `while`. You cannot forget to *reread* the next line of the file inside the loop so as not to generate an infinite loop. For example,\n", "\n", "```python\n", "line = f.readline()\n", "while line:\n", " print(line)\n", " line = f.readline()\n", "```\n", "\n", "This code will read line by line from the file and print it on the screen. When there are no more lines left, the loop will end because the `while line:` condition will be false.\n", "\n", "Once you have the line of text, you can separate each of the columns and obtain an array using the `split()` function. You have to pass the separator as a parameter of the function. For example,\n", "`text = \"1 frasalca 6.5\"\n", "list = text.split()`\n", "\n", "This code will make the variable *list* have an array with the values: `[\"1\",\"frasalca\",\"6.5\"]`. Accessing the mark can be done with `list[2]`. If you want to use any of these values as an integer or decimal number, you have to convert them with the `int()` ord `float()` functions respectively.\n", "\n", "So far, with all the previous explanation, **develop the following program**:\n", "\n", "A program that reads a text file with the students' data (identifier, user name, grade) on each line, calculates the average among all the students' grades and prints it on the screen. It must also print on screen for each student whether they have passed or not (print student name and *PASS* or *NOT PASS*). Students need a grade of 5 or more to pass.\n", "\n", "In order to be used by your program, a file *marks.csv* must first be generated with the information of at least 5 students (made-up)." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.4" } }, "nbformat": 4, "nbformat_minor": 4 }